package com.ssyu.examination.system.service;

import com.ssyu.examination.conmmon.util.DBUtil;
import com.ssyu.examination.system.entity.SystemEntiy;

public class SystemService {

	/**
	 * 登录界面
	 * 
	 * @param code
	 * @param password
	 * @return
	 */
	public SystemEntiy login(String code, String password) {
		return DBUtil.getUniqueResult(SystemEntiy.class,
				"SELECT * FROM sys_user WHERE code=? AND password=?", code,
				password);
	}

	/**
	 * 登录状态是否允许登录
	 * 
	 * @param code
	 * @param password
	 * @return
	 */
	public SystemEntiy loginState(String code, String password) {
		return DBUtil
				.getUniqueResult(
						SystemEntiy.class,
						"SELECT * FROM sys_user u,sys_role r,sys_class c WHERE r.id = u.role_id AND IF(u.class_id = c.id,c.state=1,TRUE) AND u.code =? AND u.state = 1 AND u. password =?",
						code, password);
	}

	/**
	 * 检查与原密码是否一致
	 * 
	 * @param name
	 * @param password
	 * @return
	 */
	public SystemEntiy checkPassword(String code, String password) {
		return DBUtil.getUniqueResult(SystemEntiy.class,
				"SELECT * FROM sys_user WHERE code=? AND password=?", code,
				password);
	}

	/**
	 * 检查电话号码是否重复
	 * 
	 * @param name
	 * @param password
	 * @return
	 */
	public SystemEntiy checkPhone(String code, String phone) {
		return DBUtil.getUniqueResult(SystemEntiy.class,
				"SELECT * FROM sys_user WHERE code !=? AND phone=?", code,
				phone);
	}

	/**
	 * 检查邮箱是否存在
	 * 
	 * @param code
	 * @param email
	 * @return
	 */
	public SystemEntiy checkEmail(String code, String email) {
		return DBUtil.getUniqueResult(SystemEntiy.class,
				"SELECT * FROM sys_user WHERE code !=? AND email=?", code,
				email);
	}

	/**
	 * 修改密码
	 * 
	 * @param name
	 * @param md5
	 * @return
	 */
	public int updatePassword(String code, String password) {
		return DBUtil.executeUpdate(
				"UPDATE sys_user SET password=? WHERE code=?", password, code);
	}

	/**
	 * 修改个人信息
	 * 
	 * @param code
	 * @param phone
	 * @param email
	 * @return
	 */
	public int personInfo(String code, String phone, String email) {
		return DBUtil.executeUpdate(
				"UPDATE sys_user SET phone=?,email=? WHERE code=?", phone,
				email, code);
	}

}
